//LiuZh 2017-06-23
import React, {Component} from 'react';

import {
    View,
    Image,
    Text,
    Alert
} from 'react-native';

import SettingItem from './comp.setting-Item';
import InitDB from '../database/init-db.js';
import Http from '../utils/http';
import {NavigationActions} from 'react-navigation'


var {Loading, EasyLoading} = require('react-native-easy-loading');

let imgEnter = require('../../img/enter.png');
let imgResetPwd = require('../../img/reset_pwd.png');
let imgForgetPwd = require('../../img/forget_pwd.png');
let imgClearCache = require('../../img/clear_cache.png');
let imgAboutUs = require('../../img/about_us.png');
let imgLogout = require('../../img/logout.png');

export default class HomeSettingsGroup extends Component {

    render() {
        return (
            <View>
                <Loading/>
                <SettingItem
                    marginTop={10}
                    info={'修改密码'}
                    leftSource={imgResetPwd}
                    onPress={this.resetPwd}
                    rightSource={imgEnter}/>
                <SettingItem
                    marginTop={1}
                    info={'忘记密码'}
                    onPress={this.forgetPwd}
                    leftSource={imgForgetPwd}
                    rightSource={imgEnter}/>
                <SettingItem
                    marginTop={10}
                    info={'清除缓存'}
                    onPress={this.clearCache}
                    leftSource={imgClearCache}
                    rightSource={imgEnter}/>
                <SettingItem
                    marginTop={10}
                    info={'上传诊断日志'}
                    onPress={this.uploadRealm}
                    leftSource={imgClearCache}
                    rightSource={imgEnter}/>
                <SettingItem
                    onPress={this.aboutUs}
                    marginTop={10}
                    info={'关于我们'}
                    leftSource={imgAboutUs}
                    rightSource={imgEnter}/>
                <SettingItem
                    onPress={this.logout}
                    marginTop={10}
                    info={'退出登陆'}
                    leftSource={imgLogout}
                    rightSource={imgEnter}/>
            </View>
        )
    }

    componentDidMount() {

    }

    resetPwd = () => {
        this.props.navigation.navigate('ResetPwd', {});
    }
    clearCache = () => {
        Util.showToast('缓存清除成功')
    }
    forgetPwd = () => {

        this.props.navigation.navigate('ForgetPwd', {});
    }
    aboutUs = () => {
        this.props.navigation.navigate('AboutUs');
    }

    logout = () => {
        Alert.alert('温馨提醒', '确定退出吗?', [
            {
                text: '取消',
                onPress: () => {
                }
            },
            {
                text: '确定',
                onPress: () => {
                    Promise.resolve().then(EasyLoading.show('正在退出..', 3000))
                    InitDB.logout();
                    Promise.resolve().then(EasyLoading.dismis());

                    const resetAction = NavigationActions.reset({
                        index: 0,
                        actions: [
                            NavigationActions.navigate({routeName: 'Login'})
                        ]
                    })
                    this.props.navigation.dispatch(resetAction)
                }
            }
        ])
    }

    uploadRealm = () => {
        Promise.resolve().then(EasyLoading.show('loading...', 5000));
        Http.post_dbFile(InitDB.getCurrentUser().empid)
            .then(res => {
                if (res.returnType.resultCode === '1') {
                    Util.showToast("上传成功");
                } else {
                    Util.showToast('上传失败:' + res.returnType.resultMessage);
                }
                Promise.resolve().then(EasyLoading.dismis());
            })
            .catch(err => {
                //请求失败
                Util.showToast('上传失败，错误信息:' + err);
                Promise.resolve().then(EasyLoading.dismis());
            })
    }
}


